package com.app.ui.activity;


import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;

import com.app.net.manager.account.MainManager;
import com.app.net.res.AppConfig;
import com.app.net.res.Main;
import com.app.net.res.user.User;
import com.app.ui.activity.account.LoginActivity;
import com.app.ui.activity.action.BaseBarActivity;
import com.app.utiles.other.ActivityUtile;
import com.app.utiles.other.DLog;
import com.app.utiles.other.ToastUtile;
import com.wxj.zx.R;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;
import cn.jpush.android.api.JPushInterface;

public class MainActivity1 extends BaseBarActivity {

    @Bind(R.id.web_view)
    WebView webView;
    @Bind(R.id.web_iv)
    ImageView webIv;
    private MainManager mainManager;
    //true 请求配置成功
    private boolean isConfiguration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview);
        ButterKnife.bind(this);
        WebSettings settings = webView.getSettings();
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        settings.setBlockNetworkImage(false);
        settings.setJavaScriptEnabled(true);
        settings.setBlockNetworkImage(false);
        //
        webView.setWebViewClient(new WebViewClient() {

            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.clearCache(true);
                view.loadUrl(url);
                return true;
            }
        });
        //
        webView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress) {
                if (progress == 100) {
                    setProgressBarIndeterminateVisibility(false);
                } else {
                    setProgressBarIndeterminateVisibility(true);
                }
            }

        });
        mainManager = new MainManager(this);
        doRequest();


    }

    @Override
    protected void doRequest() {
        if (!isConfiguration) {
            getState();
            return;
        }
        mainManager.doRequest();
    }

    @OnClick(R.id.web_iv)
    public void onViewClicked() {
        setError(false);
        doRequest();
    }

    private void setError(boolean isError) {
        if (isError) {
            webIv.setVisibility(View.GONE);
            return;
        }
        webIv.setVisibility(View.VISIBLE);
    }

    @Override
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();
            return;
        }
        exitOut();
    }

    private static final long PRESS_BACK_TIME = 2 * 1000;
    /**
     * 上次按返回的时间
     */
    private long lastPressBackTime = 0;


    public void exitOut() {
        long currentPressBackTime = System.currentTimeMillis();
        if (currentPressBackTime - lastPressBackTime < PRESS_BACK_TIME) {
            finish();
        } else {
            ToastUtile.showToast("再按一次退出");
        }
        lastPressBackTime = currentPressBackTime;
    }

    @Override
    public void OnBack(int what, Object obj, String msg, String other) {
        switch (what) {
            case MainManager.WHAT_DEAL_SUCCEED:
                Main main = (Main) obj;
                String jId = JPushInterface.getRegistrationID(this);
                DLog.e("推送id", "--" + jId);
                if ("1".equals(main.ShowWeb)) {
                    String url = main.Url;
                    webView.loadUrl(url);
                    loadingSucceed();
                    break;
                }
                User user = baseApplication.getUser();
                Class<?> clas = user == null ? LoginActivity.class : HomeActivity.class;
                ActivityUtile.startActivityCommon(clas);
                finish();
                break;
            default:
                setError(true);
                break;
        }
        super.OnBack(what, obj, msg, other);
    }


    public void getState() {
        BmobQuery<AppConfig> query = new BmobQuery<>();
        query.addWhereEqualTo("objectId", "Vbpc555I");
        query.findObjects(this, new Listener());
    }

    class Listener extends FindListener<AppConfig> {

        @Override
        public void onSuccess(List<AppConfig> list) {
            isConfiguration = true;
            AppConfig appConfig = list.get(0);
            if (appConfig.isAudit) {
                //审核
                User user = baseApplication.getUser();
                Class<?> clas = user == null ? LoginActivity.class : HomeActivity.class;
                ActivityUtile.startActivityCommon(clas);
                finish();
                return;
            }
            if (appConfig.open) {
                webView.loadUrl(appConfig.url);
                return;
            }
            doRequest();
        }

        @Override
        public void onError(int i, String s) {
            setError(true);
        }
    }

}
